package reflect.test.test2;


import reflect.entity.Person;

import java.lang.reflect.Field;

/**
 * @version 1.0
 * @author:罗苏琴
 * @Date 2024/12/3 上午10:25
 */
public class AccessStudent {
    public static void main(String[] args) {
     Class clz= Student.class;
        try {
            Object student=clz.newInstance();
            Field name=clz.getDeclaredField("name");
            Field age=clz.getDeclaredField("age");
            name.setAccessible(true);
            age.setAccessible(true);
            System.out.println("赋值前的姓名："+name.get(student)+"\t年龄："+age.get(student));
            //赋值
            name.set(student,"李四");
            age.set(student,20);
            System.out.println("赋值后姓名："+name.get(student)+"\t年龄："+age.get(student));
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }

    }
}
